<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<script type="text/javascript" src="scripts/boot.js"></script>
</head>
<body>
<input id="role_id" name="id" class="mini-hidden" />
<input type="button" value="全选" onclick="checkAll()"/>
<input type="button" value="取消全选" onclick="uncheckAll()"/>
<div style="padding-left:0px;padding-bottom:10px;height:auto;">
<div style="padding-left:0px;padding-bottom:10px;height:380px;">
<ul id="tree1" class="mini-tree" url="init_resource_view.do" style="width:330px;padding:5px;" 
        showTreeIcon="true" textField="name" idField="id" parentField="pid" resultAsTree="false"  
        showCheckBox="true" checkRecursive="true" expandOnLoad="false" allowSelect="false" 
        enableHotTrack="false">
</ul>
</div>
<div style="text-align:left;padding:10px;">               
    <a class="mini-button" onclick="onOk" id="on_ok" style="width:60px;">确定</a>       
    <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>       
</div>
</div>
<script type="text/javascript">
mini.parse();
var tree = mini.get("tree1");
function SetData(data) {
	data = mini.clone(data);
	mini.get("role_id").setValue(data.row.id);
	var roleId = data.row.id;
	$.post("role_resource_view.do", {id: roleId}, function(data){
		$.each(data, function(i,d){
			var node = tree.getNode(d);
			if(tree.isLeaf(node)) {
				tree.checkNode(node);
			}
		});
	}, "json");
}

function checkAll() {
    var nodes = tree.getAllChildNodes(tree.getRootNode());
    tree.checkNodes(nodes);
}
function uncheckAll() {
    var nodes = tree.getAllChildNodes(tree.getRootNode());
    tree.uncheckNodes(nodes);
}
function onCancel(e) {
    CloseWindow("cancel");
}
function CloseWindow(action) {            
    if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
    else window.close();            
}

//保存数据
function SaveData() {
	var value = tree.getValue(true);
    if (value.length == 0) {
    	mini.alert('至少分配一个权限!'); return;
    } else {
    	var vals = value.split(',');
    	var valId = '';
		$.each(vals, function(i,d){
			if(vals[i] == '0' || vals[i] == '1' || vals[i] == '2' || vals[i] == '3' || vals[i] == '4' || vals[i] == '5' || vals[i] == '6') {
	    		vals.splice(i, 1);
    		}
			valId = vals.join(",");
		});
    	var id = mini.get("role_id").getValue();
    	$.post("role_resource_save.do",{id:id,resourceIds:valId},function(data){
    		if(data.success) {
    			window.CloseOwnerWindow("succ");
    		} else {
    			mini.alert("数据错误!");
    		}
    	}, "json");
    }
}

function onOk() {
	SaveData();
	mini.get("on_ok").setEnabled(false);
}

</script>
</body>
</html>